package com.ekeyman.securecredit.controller;

import java.util.List;
import java.util.StringTokenizer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ekeymanlib.dto.SearchFilter;
import com.ekeyman.securecredit.dto.CreditCardGrid;
import com.ekeyman.securecredit.dto.SearchGrid;
import com.ekeyman.securecredit.util.JSONUtil;
import com.ekeyman.securecreditlib.business.CreditCardBO;
import com.ekeyman.securecreditlib.domain.CreditCard;

@Controller
public class CreditCardController {
	
	@Autowired
	private CreditCardBO creditCardBO;

	@RequestMapping(method=RequestMethod.POST, value="/searchCreditCards.sc" )
	public @ResponseBody SearchGrid searchCreditCards(
			@RequestParam String rows,
			@RequestParam String page,
			@RequestParam String sidx,
			@RequestParam String sord,
			@RequestParam(required=false) String filters,
			@RequestParam(required=false) String searchField,
			@RequestParam(required=false) String searchString,
			@RequestParam(required=false) String searchOper){
		
		SearchGrid searchCreditCard = new SearchGrid();
		
		Integer intRows = new Integer(rows);
		
		Integer intPage = new Integer(page);
		searchCreditCard.setPage(intPage);
		
		int to = (intRows * intPage);
	    int from = intPage - intRows;

		List<SearchFilter> searchFilters = JSONUtil.buildSearchFilter(filters);
		
		int records = ((Long)getCreditCardBO().getCreditCardCount(searchFilters)).intValue();
		searchCreditCard.setRecords(records);
		
		searchCreditCard.setRows(getCreditCardBO().gridList(searchFilters, sidx, sord, from, to));
		
		searchCreditCard.setTotal((int) Math.ceil((double) records / (double) intRows));

		return searchCreditCard;
	}
	
	@RequestMapping(value="/editCreditCards.sc", method=RequestMethod.POST)
	public @ResponseBody String editCreditCards(
			@RequestParam String oper,
			@RequestParam String id,
			@RequestParam(required=false) String creditCardNumber,
			@RequestParam(required=false) String nameOnCard,
			@RequestParam(required=false) String cardType,
			@RequestParam(required=false) String fourDigits,
			@RequestParam(required=false) String expirationDate){
		
		if (oper.equalsIgnoreCase("add")){
			CreditCard creditCard = new CreditCard();
			creditCard.setCreditCardNumber(creditCardNumber);
			creditCard.setNameOnCard(nameOnCard);
			creditCard.setCardType(cardType);
			creditCard.setFourDigits(fourDigits);
			creditCard.setExpirationDate(expirationDate);
			getCreditCardBO().saveCreditCard(creditCard);
		}
		else if (oper.equalsIgnoreCase("edit")) {
			long editId = Long.parseLong(id);
			CreditCardGrid creditCardGrid = new CreditCardGrid();
			creditCardGrid.setId(editId);
			creditCardGrid.setCreditCardNumber(creditCardNumber);
			creditCardGrid.setNameOnCard(nameOnCard);
			creditCardGrid.setCardType(cardType);
			creditCardGrid.setFourDigits(fourDigits);
			creditCardGrid.setExpirationDate(expirationDate);
			getCreditCardBO().saveExistingCreditCard(creditCardGrid);
		}
		else if (oper.equalsIgnoreCase("del")){
	        StringTokenizer ids = new StringTokenizer(id, ",");
	        while (ids.hasMoreTokens())
	        {
	          long removeId = Long.parseLong(ids.nextToken());
	          getCreditCardBO().deleteCreditCard(removeId);
	        }
		}
		return id;
	}


	public void setCreditCardBO(CreditCardBO creditCardBO) {
		this.creditCardBO = creditCardBO;
	}

	public CreditCardBO getCreditCardBO() {
		return creditCardBO;
	}
}
